#!/bin/bash
# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: donggu
set -e

set +e
git diff origin/$BUILDKITE_PULL_REQUEST_BASE_BRANCH...HEAD --name-only --diff-filter=ACDMRT \
| rg -v "external|third_party|experimental" \
| rg "(WORKSPACE|BUILD)|((\.bzl|\.c|\.cc|\.cpp|\.h|\.hpp)$)"

status=$?
set -e

# skip?
if [ $status -ne 0 ]
then
echo 'no WORKSPACE/BUILD/.bzl/.c/.cc/.cpp/.h/.hpp file changed.'
exit 0
fi


source ~/miniconda3/etc/profile.d/conda.sh
conda activate coin2_env

export CC=clang
export CXX=clang++
./cc/generate_compile_commands.sh

source ci/buildkite/pipeline-pr/scripts/git-rev.sh

echo '# ---------------------------------------------------------------------------- #'
echo "#    Diff between $BUILDKITE_PULL_REQUEST_BASE_BRANCH($BASE) and HEAD($HEAD)   #"
echo '# ---------------------------------------------------------------------------- #'

echo "Diff between origin/$BUILDKITE_PULL_REQUEST_BASE_BRANCH($BASE) and HEAD($HEAD):"

git diff origin/$BUILDKITE_PULL_REQUEST_BASE_BRANCH...HEAD --name-only --diff-filter=ACM \
| rg -v "external|third_party|experimental" \
| rg "(\.cc|\.h)$" \
|| true

echo '# ---------------------------------------------------------------------------- #'
echo '#                                Generate proto                                #'
echo '# ---------------------------------------------------------------------------- #'

bazel build @apiset//coin2:coin2_cc_proto

echo '# ---------------------------------------------------------------------------- #'
echo '#                                  Clang-Tidy                                  #'
echo '# ---------------------------------------------------------------------------- #'

git diff origin/$BUILDKITE_PULL_REQUEST_BASE_BRANCH...HEAD --name-only --diff-filter=ACM \
| rg -v "external|third_party|experimental" \
| rg "(\.cc|\.h)$" \
| parallel -k --max-args 1 clang-tidy --quiet --line-filter='\[\{\"name\"\:\"{}\"\}\]' \"{}\"
